package com.yunhai.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yunhai.common.dto.GlobalException;
import com.yunhai.common.dto.PaginationVo;
import com.yunhai.common.utils.ObjectUtil;
import com.yunhai.common.utils.ReflectionCopy;
import com.yunhai.device.listener.RecipeClient;
import com.yunhai.device.listener.StoreClient;
import com.yunhai.device.mapper.CrrMaterialScrapReportMapper;
import com.yunhai.device.service.CrrMaterialScrapReportService;
import com.yunhai.device.service.DeviceInfoService;
import com.yunhai.model.entity.crr.CrrMaterialScrapReportEntity;
import com.yunhai.model.entity.dms.DmsDeviceInfoEntity;
import com.yunhai.model.vo.crr.CrrMaterialScrapReportQuery;
import com.yunhai.model.vo.crr.CrrMaterialScrapReportVo;
import com.yunhai.model.vo.fms.FmsMaterialConfigIntern;
import com.yunhai.model.vo.tms.TmsStoreVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CrrMaterialScrapReportServiceImpl implements CrrMaterialScrapReportService {

    @Resource
    private CrrMaterialScrapReportMapper crrMaterialScrapReportMapper;
    @Resource
    private StoreClient storeClient;
    @Resource
    private RecipeClient recipeClient;
    @Resource
    private DeviceInfoService deviceInfoService;


    /**
     * 获取 CrrMaterialScrapReport 分页列表
     */
    @Transactional
    public IPage getCrrMaterialScrapReportPagination(PaginationVo paginationVo, CrrMaterialScrapReportQuery crrMaterialScrapReportQuery ) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<CrrMaterialScrapReportEntity> lqs = new LambdaQueryWrapper();
        crrMaterialScrapReportMapper.selectPage(page, lqs);

        List<CrrMaterialScrapReportEntity> record = page.getRecords();
        if( !ObjectUtil.isNotEmptyList(record)){
            return page;
        }

        //处理门店数据
        Set<Long> storeIds = record.stream().map(item->item.getStoreId()).collect(Collectors.toSet());
        //处理设备数据
        Set<Long> deviceIds = record.stream().map(item->item.getDeviceId()).collect(Collectors.toSet());
        //处理物料数据
        Set<Long> materialIds = record.stream().map(item->item.getMaterialId()).collect(Collectors.toSet());

//处理门店名称
        if( ObjectUtil.isNotEmptyList(storeIds) ){
            List<TmsStoreVo> storeList = storeClient.getTmsStoreByIds(storeIds.stream().toList());
            if( ObjectUtil.isNotEmptyList(storeList) ){
                for( CrrMaterialScrapReportEntity item : record ){
                    storeList.stream().filter(row->row.getId().equals(item.getStoreId())).findFirst().ifPresent(row->{
                        item.setStoreName(row.getName());
                    });
                }
            }
        }

        //数据设备唯一id、设备名称
        if( ObjectUtil.isNotEmptyList(deviceIds) ){
            List<DmsDeviceInfoEntity> deviceList = deviceInfoService.getDeviceInfoListByIds(deviceIds);
            if( ObjectUtil.isNotEmptyList(deviceList) ){
                for( CrrMaterialScrapReportEntity item : record ){
                    deviceList.stream().filter(row->row.getId().equals(item.getDeviceId())).findFirst().ifPresent(row->{
                        item.setMachineUniqueCode(row.getSn());
                    });
                }
            }
        }

        //处理物料id，名称
        if( ObjectUtil.isNotEmptyList(materialIds) ){
            List<FmsMaterialConfigIntern> recipeList = recipeClient.getRecipeByIds(materialIds.stream().toList());
            if( ObjectUtil.isNotEmptyList(recipeList) ){
                for( CrrMaterialScrapReportEntity item : record ){
                    recipeList.stream().filter(row->row.getId().equals(item.getMaterialId())).findFirst().ifPresent(row->{
                        item.setMaterialName(row.getName());
                        item.setMaterialCode(row.getNumber());
                    });
                }
            }
        }

        return page;
    }


    /**
     * 获取 CrrMaterialScrapReport 所有数据
     *
     */
    @Transactional
    public List<CrrMaterialScrapReportEntity> getCrrMaterialScrapReportAllList( CrrMaterialScrapReportQuery crrMaterialScrapReportQuery ) throws Exception {

        LambdaQueryWrapper<CrrMaterialScrapReportEntity> lqs = new LambdaQueryWrapper();
        List<CrrMaterialScrapReportEntity> list = crrMaterialScrapReportMapper.selectList(lqs);
        return list;
    }


    /**
     * 获取 CrrMaterialScrapReport 详情
     */
    @Transactional
    public CrrMaterialScrapReportEntity getCrrMaterialScrapReportDetail( Long id) throws Exception {

        CrrMaterialScrapReportEntity crrMaterialScrapReportEntity = crrMaterialScrapReportMapper.selectById(id);
        return crrMaterialScrapReportEntity;
    }


    /**
     * 新增 CrrMaterialScrapReport
     * */
    @Transactional
    public void addCrrMaterialScrapReport(CrrMaterialScrapReportVo crrMaterialScrapReportVo) throws Exception {

        CrrMaterialScrapReportEntity crrMaterialScrapReportEntity = new CrrMaterialScrapReportEntity();
        ReflectionCopy.copyProperties( crrMaterialScrapReportEntity, crrMaterialScrapReportVo );
        crrMaterialScrapReportEntity.setRecordDate(new Date());
        crrMaterialScrapReportMapper.insert(crrMaterialScrapReportEntity);
        return;
    }


    /**
     * 更新 CrrMaterialScrapReport
     * */
    @Transactional
    public void updateCrrMaterialScrapReport( CrrMaterialScrapReportVo crrMaterialScrapReportVo) throws Exception {

        if( crrMaterialScrapReportVo.getId() == null ){
            throw GlobalException.gen(10021, "id不存在");
        }
        CrrMaterialScrapReportEntity crrMaterialScrapReportEntity = new CrrMaterialScrapReportEntity();
        ReflectionCopy.copyProperties( crrMaterialScrapReportEntity, crrMaterialScrapReportVo );
        crrMaterialScrapReportMapper.updateById(crrMaterialScrapReportEntity);
        return;
    }

    /**
     * 删除 CrrMaterialScrapReport
     * */
    @Transactional
    public void batchRemoveCrrMaterialScrapReport( List<Long> ids ) throws Exception {

        if( !ObjectUtil.isNotEmptyList(ids) ){
            throw GlobalException.gen(10011, "要删除的id不存在");
        }
        crrMaterialScrapReportMapper.deleteBatchIds(ids);
    }

}